package wsi.gui.resolve.task;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

import javafx.concurrent.Task;

import org.rygh.core.util.UMath;

import wsi.exp.ExpFolderController;
import wsi.exp.exp5.JudgeFile;
import wsi.exp.exp5.JudgeFileManager;

public class ComputePrecisionTask extends Task<String> {

	@Override
	protected String call() throws Exception {
		updateMessage("Computing...");

		File dir = ExpFolderController.shared().getFolderForExp5();

		List<Double> precisions = new LinkedList<>();
		int c = 0, t = 0;

		for (File ws : dir.listFiles()) {
			if (!ws.isDirectory())
				continue;

			for (File fdir : ws.listFiles()) {
				if (!fdir.isDirectory())
					continue;

				t = c = 0;

				for (File f : fdir
						.listFiles(JudgeFileManager.EXP2B_JUDGED_FILE)) {
					JudgeFile jf = new JudgeFile(f);

					t += jf.getJudgedSize();
					c += jf.getCorrectSize();
				}

				if (t > 0)
					precisions.add(1. * c / t);
			}
		}

		double p = UMath.getAverage(precisions, 0.) * 100.;

		String res = String.format("Precision: %.2f%%", p);

		updateMessage(res);

		return res;
	}

}
